Floyd–Rivest Algorithm
   HOME

TheInfoList



OR:

In
computer science Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to Applied science, practical discipli ...
, the Floyd-Rivest algorithm is a
selection algorithm In computer science, a selection algorithm is an algorithm for finding the ''k''th smallest number in a list or array; such a number is called the ''k''th ''order statistic''. This includes the cases of finding the minimum, maximum, and median el ...
developed by
Robert W. Floyd Robert W Floyd (June 8, 1936 – September 25, 2001) was a computer scientist. His contributions include the design of the Floyd–Warshall algorithm (independently of Stephen Warshall), which efficiently finds all shortest paths in a graph and ...
and
Ronald L. Rivest Ronald Linn Rivest (; born May 6, 1947) is a cryptographer and an Institute Professor at MIT. He is a member of MIT's Department of Electrical Engineering and Computer Science (EECS) and a member of MIT's Computer Science and Artificial Inte ...
that has an optimal expected number of comparisons within
lower-order terms The leading-order terms (or corrections) within a mathematics, mathematical equation, Expression (mathematics), expression or mathematical model, model are the Term (logic), terms with the largest order of magnitude.J.K.Hunter, ''Asymptotic Analysis ...
. It is functionally equivalent to
quickselect In computer science, quickselect is a selection algorithm to find the ''k''th smallest element in an unordered list. It is also known as the kth order statistics . It is related to the quicksort sorting algorithm. Like quicksort, it was devel ...
, but runs faster in practice on average. It has an expected running time of and an expected number of comparisons of . The algorithm was originally presented in a Stanford University technical report containing two papers, where it was referred to as SELECT and paired with PICK, or
median of medians In computer science, the median of medians is an approximate (median) selection algorithm, frequently used to supply a good pivot for an exact selection algorithm, mainly the quickselect, that selects the ''k''th smallest element of an initially u ...
. It was subsequently published in
Communications of the ACM ''Communications of the ACM'' is the monthly journal of the Association for Computing Machinery (ACM). It was established in 1958, with Saul Rosen as its first managing editor. It is sent to all ACM members. Articles are intended for readers with ...
, Volume 18: Issue 3.


Algorithm

The Floyd-Rivest algorithm is a
divide and conquer algorithm In computer science, divide and conquer is an algorithm design paradigm. A divide-and-conquer algorithm recursively breaks down a problem into two or more sub-problems of the same or related type, until these become simple enough to be solved dire ...
, sharing many similarities with
quickselect In computer science, quickselect is a selection algorithm to find the ''k''th smallest element in an unordered list. It is also known as the kth order statistics . It is related to the quicksort sorting algorithm. Like quicksort, it was devel ...
. It uses sampling to help partition the list into three sets. It then recursively selects the ''k''th smallest element from the appropriate set. The general steps are: # Select a small random sample ''S'' from the list ''L''. # From ''S'', recursively select two elements, ''u'' and ''v'', such that ''u'' < ''v''. These two elements will be the pivots for the partition and are expected to contain the ''k''th smallest element of the entire list between them (in a sorted list). # Using ''u'' and ''v'', partition ''S'' into three sets: ''A'', ''B'', and ''C''. ''A'' will contain the elements with values less than ''u'', ''B'' will contain the elements with values between ''u'' and ''v'', and ''C'' will contain the elements with values greater than ''v''. # Partition the remaining elements in ''L'' (that is, the elements in ''L'' - ''S'') by comparing them to ''u'' or ''v'' and placing them into the appropriate set. If ''k'' is smaller than half the number of the elements in ''L'' rounded up, then the remaining elements should be compared to ''v'' first and then only to ''u'' if they are smaller than ''v''. Otherwise, the remaining elements should be compared to ''u'' first and only to ''v'' if they are greater than ''u''. # Based on the value of ''k'', apply the algorithm recursively to the appropriate set to select the ''k''th smallest element in ''L''. By using , ''S'', = Θ(''n''2/3 log1/3 ''n''), we can get expected comparisons. We can get expected comparisons by starting with a small ''S'' and repeatedly updating ''u'' and ''v'' to keep the size of ''B'' small enough (''O''(''n''1/2 log1/2 ''n'') at Θ(''n'') processed elements) without unacceptable risk of the desired element being outside of ''B''.


Pseudocode version

The following
pseudocode In computer science, pseudocode is a plain language description of the steps in an algorithm or another system. Pseudocode often uses structural conventions of a normal programming language, but is intended for human reading rather than machine re ...
rearranges the elements between left and right, such that for some value ''k'', where left ≤ ''k'' ≤ right, the ''k''th element in the list will contain the (''k'' − left + 1)th smallest value, with the ith element being less than or equal to the ''k''th for all left ≤ i ≤ k and the jth element being larger or equal to for k ≤ j ≤ right: // ''left is the left index for the interval'' // ''right is the right index for the interval'' // ''k is the desired index value, where array is the (k+1)th smallest element when left = 0'' function select(array, left, right, k) is while ''right'' > ''left'' do // ''Use select recursively to sample a smaller set of size s'' // ''the arbitrary constants 600 and 0.5 are used in the original'' // ''version to minimize execution time.'' if right − left > 600 then n := right − left + 1 i := k − left + 1 z := ln(n) s := 0.5 × exp(2 × z/3) sd := 0.5 × sqrt(z × s × (n − s)/n) × sign(i − n/2) newLeft := max(left, k − i × s/n + sd) newRight := min(right, k + (n − i) × s/n + sd) select(array, newLeft, newRight, k) // ''partition the elements between left and right around t'' t := array i := left j := right swap array
eft A newt is a salamander in the subfamily Pleurodelinae. The terrestrial juvenile phase is called an eft. Unlike other members of the family Salamandridae, newts are semiaquatic, alternating between aquatic and terrestrial habitats. Not all aqu ...
and array if array ight> t then swap array ightand array
eft A newt is a salamander in the subfamily Pleurodelinae. The terrestrial juvenile phase is called an eft. Unlike other members of the family Salamandridae, newts are semiaquatic, alternating between aquatic and terrestrial habitats. Not all aqu ...
while i < j do swap array and array i := i + 1 j := j − 1 while array < t do i := i + 1 while array > t do j := j − 1 if array
eft A newt is a salamander in the subfamily Pleurodelinae. The terrestrial juvenile phase is called an eft. Unlike other members of the family Salamandridae, newts are semiaquatic, alternating between aquatic and terrestrial habitats. Not all aqu ...
= t then swap array
eft A newt is a salamander in the subfamily Pleurodelinae. The terrestrial juvenile phase is called an eft. Unlike other members of the family Salamandridae, newts are semiaquatic, alternating between aquatic and terrestrial habitats. Not all aqu ...
and array else j := j + 1 swap array and array ight // ''Adjust left and right towards the boundaries of the subset'' // ''containing the (k − left + 1)th smallest element.'' if j ≤ k then left := j + 1 if k ≤ j then right := j − 1


See also

*
Quickselect In computer science, quickselect is a selection algorithm to find the ''k''th smallest element in an unordered list. It is also known as the kth order statistics . It is related to the quicksort sorting algorithm. Like quicksort, it was devel ...
*
Introselect In computer science, introselect (short for "introspective selection") is a selection algorithm that is a hybrid of quickselect and median of medians which has fast average performance and optimal worst-case performance. Introselect is related t ...
*
Median of medians In computer science, the median of medians is an approximate (median) selection algorithm, frequently used to supply a good pivot for an exact selection algorithm, mainly the quickselect, that selects the ''k''th smallest element of an initially u ...


References

* * * {{DEFAULTSORT:Floyd-Rivest algorithm Selection algorithms